package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ContractList;


public class ContractListManager extends HibernateDaoSupport {

	public void addContractList(ContractList contractList) {
		
		getHibernateTemplate().save(contractList);
	}
	
	public void updateContractList(ContractList contractList) {
		
		getHibernateTemplate().update(contractList);
	}
	
	public void deleteContractList(int id){
		
		getHibernateTemplate().delete(getContractList(id));
	}
	
	public ContractList getContractList(int id){
		ContractList d =(ContractList)getHibernateTemplate().get(ContractList.class, id);
		return d;
	}
	
	@SuppressWarnings("unchecked")
	public ContractList getContractList(String listNum){
		List <ContractList>list = getHibernateTemplate().find("from ContractList c where c.listNum = '" + listNum + "'" );
		ContractList d = list.get(0);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<ContractList> listContractList() {
		return getHibernateTemplate().find("from ContractList");
	}
	
	@SuppressWarnings("unchecked")
	public List<ContractList> listContractList(String query) {
		return getHibernateTemplate().find("from ContractList c where c.listNum like '%" + query + "%'");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <ContractList>list = getHibernateTemplate().find("from ContractList" );
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ContractList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	//自动添加对应表格的单号
	@SuppressWarnings("unchecked")	
	public String listNum(){
		List <ContractList>list = getHibernateTemplate().find("from ContractList" );
		int i = list.size();
		if(i==0){
			return "000001";
		}else{
			String tmp = list.get(i-1).getListNum();
			while(tmp.indexOf(0) == '0'){
				tmp = tmp.substring(1);
			}
			int t = Integer.parseInt(tmp) + 1;
			tmp = t + "";
			for(int j=6;j>tmp.length();){
				tmp ="0"+tmp;
			}
			return tmp;
		}
		
		
	}
	


}



